//
//  FGNavigatioController.swift
//  FGApp
//
//  Created by ryan on 2019/4/26.
//  Copyright © 2019 ryan. All rights reserved.
//

import UIKit
import SVProgressHUD

class FGNavigatioController: UINavigationController {
    

    /// 0:默认  1 : push   2 : pop

    static let change_key : String = "CHANGE_KEY"
    
    func addObserves() {
        
    }
    
    ///
    override var preferredStatusBarStyle: UIStatusBarStyle{
        get{
            return .default
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        UserDefaults.standard.set(0, forKey: FGNavigatioController.change_key)
        addObserves()
        navigationBar.isTranslucent = false
        navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font:UIFont.systemFont(ofSize: 17)]
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.shadowImage = UIImage()
//        self.modalPresentationStyle = .fullScreen
    }
    
    func imageWithColor(_ color: UIColor) -> UIImage? {
        let rect = CGRect (x: 0, y: 0, width: 1, height: 1)
        
        UIGraphicsBeginImageContext(rect.size)
        
        let ctx = UIGraphicsGetCurrentContext()
        
        ctx?.setFillColor(color.cgColor)
        
        ctx?.fill(rect)
        
        let ig = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return ig
        
    }
    
    
    
    func colorWith(_ controller: UIViewController) -> UIColor {
        
        let b = controller.responds(to: Selector.init(("t_barTintColor")))
        
        var color = UC_Default_Bar
        
        if b {
            color = (controller.value(forKey: "t_barTintColor") as? UIColor) ?? UC_Default_Bar
        }
        
        return color
    }
    
    private func judgeTabbar2(_ state : Int) {


        if UserDefaults.standard.integer(forKey: FGNavigatioController.change_key) == 1 {

            return
        }
        if self.tabBarController != nil {
            if viewControllers.count <= 2 {
                if state == 0 {
                    let tabbar = self.tabBarController as! FGTabBarController
                    tabbar.setTabBarHidden(isHidden: true)
                }else {
                    let tabbar = self.tabBarController as! FGTabBarController
                    tabbar.setTabBarHidden(isHidden: false)
                }
            }
            
        }
    }
    
    /*
    private func judgeTabbar() {
        if self.tabBarController != nil {
            let tabbar = self.tabBarController as! FGTabBarController
            if UserDefaults.standard.integer(forKey: change_key) == 1 {
                if viewControllers.count > 0 {
                    tabbar.setTabBarHidden(isHidden: true)
                }
            }else if UserDefaults.standard.integer(forKey: change_key) == 2 {
                
                if viewControllers.count <= 2 {
                    tabbar.setTabBarHidden(isHidden: false)
                }
            }
            // 归位
            UserDefaults.standard.set(0, forKey: change_key)
        }
    }*/
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
//        print("================================ PUSH Controller 层数 :  \(viewControllers.count) ======================")
        
        if viewControllers.count > 0 && self.tabBarController != nil {
//            viewController.hidesBottomBarWhenPushed = true
            
            let tabbar = self.tabBarController as! FGTabBarController
            tabbar.setTabBarHidden(isHidden: true)
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem (image: UIImage (named: "checkUserType"), style: .plain, target: self, action: #selector(navigationBackClick))

            UserDefaults.standard.set(1, forKey: FGNavigatioController.change_key)
        }
        super.pushViewController(viewController, animated: true)
    }
    
    override func popViewController(animated: Bool) -> UIViewController? {
        
//        print("================================ POP Controller 层数 :  \(viewControllers.count) ======================")
        
        if viewControllers.count == 2 && self.tabBarController != nil {
            
            let tabbar = self.tabBarController as! FGTabBarController
            tabbar.setTabBarHidden(isHidden: false)
            

            UserDefaults.standard.set(2, forKey: FGNavigatioController.change_key)

        }
        
        return super.popViewController(animated: animated)
    }
    
    
    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        let tabbar = self.tabBarController as! FGTabBarController
        tabbar.setTabBarHidden(isHidden: false)
        
        return super.popToRootViewController(animated: animated)
    }
    

    
    @objc func navigationBackClick() {
        if SVProgressHUD.isVisible() {
            SVProgressHUD.dismiss()
        }
        
        if UIApplication.shared.isNetworkActivityIndicatorVisible {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
        }
        
        popViewController(animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
